//
//  ShellAccountViewModel.m
//  ShellWallert
//
//  Created by lvzhao on 2019/10/4.
//  Copyright © 2019 VV. All rights reserved.
//

#import "ShellAccountViewModel.h"
#import "LZBaseTabBarViewController.h"
#import "SocketRocketUtility.h"
#import "JPUSHService.h"

@implementation ShellAccountViewModel
/**
 Convert 登錄的接口
 @param account 邮箱
 @param password 密码
 */
- (void) loginAccount:(NSString *)account password:(NSString *)password{
    
    //手机型号
    NSString *phoneModel = LZTool.getCurrentDeviceModel;
    NSDictionary *params = @{@"email":account?:@"",
                             @"password":password?:@"",
                             @"device":phoneModel?:@""};
    
    [LZNetworkingManager lz_request:@"post" url:kLogin params:params success:^(id  _Nullable responseObject) {

        showDelayedDismissTitle(LZLocalizedString(@"登录成功"), LZCurrentView);
        NSString *token = responseObject[kResultData];
        ShellUserDetailModel *detailModel = [LZToolCache getUserInfo];
        detailModel.token = token;
        [LZToolCache saveUserInfo:detailModel];
        
        
        ///登陆成功
        [[NSNotificationCenter defaultCenter] postNotificationName:k_APPLoginSuccessNotificationName object:nil];
        
        //获取个人信息
        [LZNetworkingManager lz_request:@"get" url:kUserInfo params:nil success:^(id  _Nullable responseObject) {
            if([responseObject[kResultStatus] isEqualToString:kCode]){
                NSDictionary *dataDictionary = responseObject[kResultData];
                ShellUserDetailModel *infoModel = [LZToolCache getUserInfo];
                infoModel.name = dataDictionary[@"name"];
                infoModel.email = dataDictionary[@"email"];
                infoModel.portrait = dataDictionary[@"portrait"];
                infoModel.myinviteCode = dataDictionary[@"myinviteCode"];
                infoModel.showLevel = dataDictionary[@"showLevel"];
                [LZToolCache saveUserInfo:infoModel];
                
                
                NSString *userId = [@"" lz_checkNULLString:dataDictionary[@"id"]];
                if(userId.length == 0)userId = @"test0000";
                [JPUSHService setAlias:userId completion:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
                      
                } seq:100];
            }
            
            
            
        } failure:^(NSError * _Nullable error) {}];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.8 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [LZTool.currentViewController.navigationController dismissViewControllerAnimated:YES completion:nil];

        });
    } failure:^(NSError * _Nullable error) {} isLoading:YES isFailTip:YES];
}





/**
 Convert 发送短信接口
 @param account 邮箱
 @param type  A 注册   B
 
 */
- (void) sendVerifiCode:(NSString *)account
                   type:(NSInteger)type{
    
    
    NSString *url = @"";
    switch (type) {
        case 1:
            url = kSendRegisterEmail;
            break;
        case 2:
            url = kSendResetPasswordEmail;
            break;
        case 3:
            url = ksendResetPayPasswordEmail;
            break;
        default:
            break;
    }
    
    
    NSDictionary *params = @{@"email":account?:@""};
    
    [LZNetworkingManager lz_request:@"post" url:url params:params success:^(id  _Nullable responseObject) {
        
        if([responseObject[kResultStatus] isEqualToString:kCode]){
            
//            NSString *tip = [NSString stringWithFormat:@"%@",responseObject[kResultData]];
            showDelayedDismissTitle(LZLocalizedString(@"验证码发送成功,请注意查收"), nil);
//            showDelayedDismissTitle(tip, nil);
            [self.reloadSubject sendNext:@(1)];
        }
    } failure:^(NSError * _Nullable error) {} isLoading:YES isFailTip:YES];
}




/**
 Convert  註冊的接口
 @param account  账号
 @param areaCode  国家
 @param invitCode  邀請碼
 @param verification  驗證碼
 @param password 密碼
 */
- (void) registerAccount:(NSString *)account
                areaCode:(NSString *)areaCode
               invitCode:(NSString *)invitCode
            verification:(NSString *)verification
                password:(NSString *)password{
    
    //手机型号
    NSString *phoneModel = LZTool.getCurrentDeviceModel;
    NSDictionary *params = @{@"email":account?:@"",
                             @"country":areaCode?:@"中国",
                             @"veriCode":verification?:@"",
                             @"inviteCode":invitCode?:@"",
                             @"source":@"iOS",
                             @"password":password?:@"",
                             @"device":phoneModel?:@""};
    [LZNetworkingManager lz_request:@"post" url:kRegister params:params success:^(id  _Nullable responseObject) {
        if([responseObject[kResultStatus] isEqualToString:kCode]){
            showDelayedDismissTitle(LZLocalizedString(@"注册成功"), LZCurrentView);
            NSString * token = responseObject[kResultData];
            ShellUserDetailModel *detailModel = [LZToolCache getUserInfo];
            detailModel.token = token;
            [LZToolCache saveUserInfo:detailModel];
            
            ///登陆成功
            [[NSNotificationCenter defaultCenter] postNotificationName:k_APPLoginSuccessNotificationName object:nil];
        
            
            //获取个人信息
            [LZNetworkingManager lz_request:@"get" url:kUserInfo params:nil success:^(id  _Nullable responseObject) {
                if([responseObject[kResultStatus] isEqualToString:kCode]){
                    NSDictionary *dataDictionary = responseObject[kResultData];
                    ShellUserDetailModel *infoModel = [LZToolCache getUserInfo];
                    infoModel.name = dataDictionary[@"name"];
                    infoModel.email = dataDictionary[@"email"];
                    infoModel.portrait = dataDictionary[@"portrait"];
                    infoModel.myinviteCode = dataDictionary[@"myinviteCode"];
                    infoModel.showLevel = dataDictionary[@"showLevel"];
                    [LZToolCache saveUserInfo:infoModel];
                    
                    NSString *userId = [@"" lz_checkNULLString:dataDictionary[@"id"]];
                    if(userId.length == 0)userId = @"test0000";
                    [JPUSHService setAlias:userId completion:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {} seq:100];
                }
                
                
            } failure:^(NSError * _Nullable error) {}];
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.8 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [LZTool.currentViewController.navigationController dismissViewControllerAnimated:YES completion:nil];
            });
        }
    } failure:^(NSError * _Nullable error) {} isLoading:YES isFailTip:YES];
}


/**
 Convert  忘记密码的接口
 @param account  账号
 @param verification  驗證碼
 @param password 密碼
 */
- (void) forgetAccount:(NSString *)account
          verification:(NSString *)verification
              password:(NSString *)password{
    
    NSDictionary *params = @{@"email":account?:@"",
                             @"veriCode":verification?:@"",
                             @"newPassword":password?:@""};
    [LZNetworkingManager lz_request:@"post" url:kResetPasswordForget params:params success:^(id  _Nullable responseObject) {
        if([responseObject[kResultStatus] isEqualToString:kCode]){
            showDelayedDismissTitle(LZLocalizedString(@"修改成功"), LZCurrentView);
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.8 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

                [LZTool.currentViewController.navigationController popViewControllerAnimated:YES];
            });
        }
    } failure:^(NSError * _Nullable error) {} isLoading:YES isFailTip:YES];

}


/**
 Convert  修改登录密码
 @param account  账号
 @param password 密碼
 */
- (void) changeLoginAccount:(NSString *)account
                   password:(NSString *)password{
    
    NSDictionary *params = @{@"oldPassword":account?:@"",
                             @"newPassword":password?:@""};
    [LZNetworkingManager lz_request:@"post" url:kUserResetPassword params:params success:^(id  _Nullable responseObject) {
        if([responseObject[kResultStatus] isEqualToString:kCode]){
            showDelayedDismissTitle(LZLocalizedString(@"修改成功"), LZCurrentView);
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.8 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                
                [LZTool.currentViewController.navigationController popViewControllerAnimated:YES];
            });
        }
    } failure:^(NSError * _Nullable error) {} isLoading:YES isFailTip:YES];

}

/**
 Convert 发送修改交易密码
 @param account 邮箱
 */
- (void) sendChangTradePwd:(NSString *)account{
    
    NSDictionary *params = @{@"email":account?:@""};
    
    [LZNetworkingManager lz_request:@"post" url:kSendRegisterEmail params:params success:^(id  _Nullable responseObject) {
        
        if([responseObject[kResultStatus] isEqualToString:kCode]){
            showDelayedDismissTitle(LZLocalizedString(@"验证码发送成功,请注意查收"), nil);
            [self.reloadSubject sendNext:@(1)];
        }
    } failure:^(NSError * _Nullable error) {} isLoading:YES isFailTip:YES];
}

/**
 Convert  修改交易密码
 @param veriCode 验证码
 @param password 密碼
 */
- (void) changeTradeVeriCode:(NSString *)veriCode
                    password:(NSString *)password{
    
    NSDictionary *params = @{@"veriCode":veriCode?:@"",
                             @"payPassword":password?:@""};
    [LZNetworkingManager lz_request:@"post" url:kUserResetPayPassword params:params success:^(id  _Nullable responseObject) {
        if([responseObject[kResultStatus] isEqualToString:kCode]){
            showDelayedDismissTitle(LZLocalizedString(@"修改成功"), LZCurrentView);
            
            ShellUserDetailModel *detailModel = [LZToolCache getUserInfo];
            detailModel.payPasswordFlag = YES;
            [LZToolCache saveUserInfo:detailModel];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.8 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                
                [LZTool.currentViewController.navigationController popViewControllerAnimated:YES];
            });
        }
    } failure:^(NSError * _Nullable error) {} isLoading:YES isFailTip:YES];
}

@end
